home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 2: CDPD 1 / Almathera Ten on Ten - Disc 2: CDPD 1.iso / pd / 076-100 / 084 / ed / bitmap.c < prev    next >
C/C++ Source or Header  |  1995-03-13  |  1KB  |  78 lines

  1. /*
  2.  *    BITMAP.C -    makebitmap, setbit, testbit
  3.  *            bit-map manipulation routines.
  4.  *
  5.  *    Copyright (c) Allen I. Holub, all rights reserved.  This program may
  6.  *        for copied for personal, non-profit use only.
  7.  *
  8.  */
  9.  
  10. #ifdef DEBUG
  11. #include <stdio.h>
  12. #endif
  13.  
  14. #include "tools.h"
  15.  
  16.  
  17. BITMAP    *makebitmap( size )
  18. unsigned size;
  19. {
  20.     /*    Make a bit map with "size" bits.  The first entry in
  21.      *    the map is an "unsigned int" representing the maximum
  22.      *    bit.  The map itself is concatenated to this integer.
  23.      *    Return a pointer to a map on success, 0 if there's
  24.      *    not enough memory.
  25.      */
  26.  
  27.     unsigned *map, numbytes;
  28.     extern char *malloc ();
  29.  
  30.     numbytes = (size >> 3) + ((size & 0x07) ? 1 : 0 );
  31.  
  32. #ifdef DEBUG
  33.     printf("Making a %d bit map (%d bytes required)\n", size, numbytes);
  34. #endif
  35.  
  36.     if( map = (unsigned *) malloc( numbytes + sizeof(unsigned) ))
  37.         *map = size;
  38.  
  39.     return ((BITMAP *)map);
  40. }
  41.  
  42. setbit( c, map, val )
  43. unsigned    c, val;
  44. char        *map;
  45. {
  46.     /*    Set bit c in the map to val.
  47.      *    If c > map-size, 0 is returned, else 1 is returned.
  48.      */
  49.  
  50.     if( c >= *(unsigned *)map )    /* if c >= map size */
  51.         return 0;
  52.  
  53.     map += sizeof(unsigned);    /* skip past size */
  54.     
  55.     if( val )
  56.         map[c >> 3] |= 1 << (c & 0x07);
  57.     else
  58.         map[c >> 3] &= ~(1 << (c & 0x07));
  59.  
  60.     return( 1 );
  61. }
  62.  
  63. testbit( c, map )
  64. unsigned    c;
  65. char        *map;
  66. {
  67.     /*    Return 1 if the bit corresponding to c in map is set.
  68.      *    0 if it is not.
  69.      */
  70.  
  71.     if( c >= *(unsigned *)map )
  72.         return 0;
  73.  
  74.     map += sizeof(unsigned);
  75.     
  76.     return(map[ c >> 3 ] & (1 << (c & 0x07)));
  77. }
  78.